###########
# Replication Data for
# Fast and Accurate Estimation of Non-Nested
# Binomial Hierarchical Models Using Variational
# Inference
# By Max Goplerud. Forthcoming at Bayesian Analysis.
# arxiv link: https://arxiv.org/abs/2007.12300
###########

To begin, you will need to install the following two packages from GitHub.

# The exact version used can be found with 'cd582f3' or 'edc900d' (needed for script 8)
remotes::install_github('mgoplerud/vglmer')
remotes::install_github('vdorie/blme')

Next, you will need to create the underlying Ghitza/Gelman data. To do this, please download their replication from Dataverse (https://doi.org/10.7910/DVN/PZAOO6), put into a folder (e.g. "build_gg") and run the following script "0.prepare_GG.R". This will save an RDS file into the "data" folder that can be loaded for all of the subsequent analyses.

The scripts in the "code" folder do the following things. Each script will save the output in the "models" or "output" folder to be used in creating the figures. Set your working directory to a folder that contains all of the sub-folders in the .zip.
	- 0.prepare_GG.R: Takes the Ghitza/Gelman replication data and turns it into a cleaner RDS form for subsequent analysis.
	- 1.estimate_gold.R: Estimates the "gold standard" models using a modification of brms to allow for an Inverse Wishart prior (brms) and a Laplace approximation with a prior (blme). This will take a while to run.
	- 2.estimate_vglmer.R: Estimates the variational models (vglmer) with all three factorization schemes and MAVB to get the raw output used for performance.
	- 3.cv_vglmer.R: Performs 10-fold cross-validation with Scheme I to compare which model seems to fit the Ghitza and Gelman data best.
	- 4.cv_gold.R: Fits Models 1, 4, and 9 using brms and bglmer as well as the variational methods with classical 10-fold cross-validation. Takes nearly ten days to run!
	- 5.bayesian_WAIC.R: Calculates the WAIC and PSIS for each Bayesian model.
	- 6.sim_synth.R: Runs the simulations on synthetic data reported in the main text.
	- 7.sim_bootstrap.R: Runs the simulations on the bootstrapped data.
	- 8.pxvb.R: Shows the impact of PX-VB
	- 9.random_init.R: Shows the stability to choice of initial values.
	- 10.make_figures: After running all of the above scripts, collects and gathers the data to build the tables and figures for the paper and the supporting information.
